mongo-mapreduce测试(1)——count/sum/where条件

mongo-mapreduce测试(1)——count/sum/where条件

mongo-mapreduce测试(2)——列转行与finalize函数

mongo-mapreduce测试(3)——group by having

mongo-mapreduce测试(4)——avg

mongo-mapreduce测试(5)——max/min

mongo-mapreduce测试(6)——综合测试

mongo-mapreduce测试(7)——使用js存储过程

mongo-mapreduce测试(8)——阶段总结(1)

mongo-mapreduce测试(9)——python调用

mongo-mapreduce测试(10)——阶段总结(2)

mongo-mapreduce测试(11)——跟踪调试

1. 准备测试数据

> db.tianyc_test3.insert({name:'xtt',dic:1})
> db.tianyc_test3.insert({name:'xtt',dic:2})
> db.tianyc_test3.insert({name:'xtt',dic:3})
> db.tianyc_test3.insert({name:'xtt',dic:4})
> db.tianyc_test3.insert({name:'xtt',dic:5})
> db.tianyc_test3.insert({name:'yct',dic:1})
> db.tianyc_test3.insert({name:'yct',dic:2})
> db.tianyc_test3.insert({name:'neu',dic:1})
> db.tianyc_test3.insert({name:'neu',dic:2})
> db.tianyc_test3.insert({name:'neu',dic:3})
> db.tianyc_test3.insert({name:'neu',dic:4})
> db.tianyc_test3.find()
{ "_id" : ObjectId("51258e47ff13bc6383473137"), "name" : "xtt", "dic" : 1 }
{ "_id" : ObjectId("51258e49ff13bc6383473138"), "name" : "xtt", "dic" : 2 }
{ "_id" : ObjectId("51258e4bff13bc6383473139"), "name" : "xtt", "dic" : 3 }
{ "_id" : ObjectId("51258e4eff13bc638347313a"), "name" : "xtt", "dic" : 4 }
{ "_id" : ObjectId("51258e50ff13bc638347313b"), "name" : "xtt", "dic" : 5 }
{ "_id" : ObjectId("51258e5aff13bc638347313c"), "name" : "yct", "dic" : 1 }
{ "_id" : ObjectId("51258e5cff13bc638347313d"), "name" : "yct", "dic" : 2 }
{ "_id" : ObjectId("51258e62ff13bc638347313e"), "name" : "neu", "dic" : 1 }
{ "_id" : ObjectId("51258e64ff13bc638347313f"), "name" : "neu", "dic" : 2 }
{ "_id" : ObjectId("51258e65ff13bc6383473140"), "name" : "neu", "dic" : 3 }
{ "_id" : ObjectId("51258e68ff13bc6383473141"), "name" : "neu", "dic" : 4 }

2. count

实现 select name, count(*) from tianyc_test3 group by name;

方法1:使用计数器累计

> var map = function() {
... emit(this.name, {cnt: 1});#设置cnt虚拟列,将每个collection的cnt设置为1。
... };
> var reduce = function(key, values) {
... var count = 0;
... for (var i=0;i<values.length;i++){
... count+=values[i].cnt;#对每个name的所有cnt进行相加,得到count结果
... }
... return count;
... };
> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result"});
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : 4 }
{ "_id" : "xtt", "value" : 5 }
{ "_id" : "yct", "value" : 2 }

方法2:直接使用map后的数组长度。

map函数可以不变,也可再简化:

> var map = function() {
... emit(this.name,1);
... };

reduce函数修改如下:

> var reduce = function(key, values) {
... return values.length;
... };
> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result"});
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : 4 }
{ "_id" : "xtt", "value" : 5 }
{ "_id" : "yct", "value" : 2 }

3. 带where条件的count

实现 select name, count(*) from tianyc_test3 where dic>1 group by name;

方法1:使用mapReduce函数的query参数

> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {query:{dic:{$gt:1}}, out: "tianyc_test3_result"});
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : 3 }
{ "_id" : "xtt", "value" : 4 }
{ "_id" : "yct", "value" : { "cnt" : 1 } }

方法2:在map函数中过滤。reduce函数不变,map函数如下:

> var map = function() {
... if (this.dic>1){
... emit(this.name, {cnt: 1});
... }
... };

> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result"});
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : 3 }
{ "_id" : "xtt", "value" : 4 }
{ "_id" : "yct", "value" : { "cnt" : 1 } }

4. sum

实现 select name, sum(dic) from tianyc_test3 group by name

> var map = function() {
... emit(this.name, {score: this.dic});#设置score虚拟列,将每个collection的score设置为dic的值,用于reduce相加。
... };
> var reduce = function(key, values) {
... var sum = 0;
... for (var i=0;i<values.length;i++){
... sum+=values[i].score;#对每个name的所有score进行相加,得到sum结果。
... }
... return sum;
... };
> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});
> db.tianyc_test3_result2.find()
{ "_id" : "neu", "value" : 10 }
{ "_id" : "xtt", "value" : 15 }
{ "_id" : "yct", "value" : 3 }

 

posted @ 2013-02-21 11:19  醇酒醉影  阅读(6777)  评论(4编辑  收藏  举报